<?php 
include('../Connections/KRAPPERCON.php');
include('../Connections/Accesos.php');

$obj = 'GES_USR';


$found = false;

//$msg = checkObj($user_now,'MN_USERS');
if ($msg!= ''){
?>
<script type="text/javascript">
showError('<?php echo $msg; ?>');
</script>
<?php 
exit;
}

include('../Query_Min/getAlmacenes.php');
$combo_alm = $combo;
?>
<div> 

&nbsp;<input type="button" value="Actulizar Lista" onclick="updListaUsr()" /></div>
<div id="d_tbl_usr">
<table width="100%" cellspacing="0" class="tableIn" border="1" rules="all" cellpadding="0">
<thead>
  <tr>
    <th>Usuario</th>
    <th>Nombres y Apellidos</th>
    <th>DNI</th>
    <th>Centro</th>
    <th>Almacen</th>
    <th>Status</th>
    <th>&nbsp;</th>
    </tr>
  </thead>
  <tbody>
  <?php 
  $sql = "call sp_getAllUsers('')";
  $res = $mysqli->query($sql);
  if ($res){
  	while($row = $res->fetch_assoc()){
	?>
    <tr>
    <td><a href="#t_usr_usr" onClick="editUserSis(this)"><?php echo $row['UserID']; ?></a></td>
    <td><?php echo $row['UserNom']." ".$row['UserApe']; ?></td>
    <td><?php echo $row['UserNIF']; ?></td>
    <td align="center"><?php echo $row['CentroNom']; ?></td>
    <td align="center"><?php echo $row['AlmacenNom']; ?></td>
    <td><?php if($row['UserStatus']==0) echo "Inactivo"; else echo "Activo"; ?></td>
    <td>
      <a href="#" onclick="resetPWD('<?php echo $row['UserID'];?>');">Nueva Clave</a>
  </td>
  </tr>
    <?php 
	}
  $res->close();
  $mysqli->check_result();
  }
  ?>
  </tbody>
</table>
</div>
<br>

<div id="d_dt_usr">
<div ><input type="button" value="Nuevo" onclick="newUser()" />&nbsp;
<!-- <input type="button" value="Editar" />&nbsp;-->
<input type="button" onclick="saveUser()" value="Grabar" /></div>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tblSort" id="tbl_dat_usr">
  <tr>
    <td>Usuario</td>
    <td>
      <input class="clsReq" name="t_usr_usr" type="text" id="t_usr_usr" size="20" maxlength="15">
    </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Nombres</td>
    <td>
      <input type="text" class="clsReq" name="t_usr_nom" id="t_usr_nom">
    </td>
    <td>Apellidos</td>
    <td>
      <input type="text" class="clsReq" name="t_usr_ape" id="t_usr_ape">
    </td>
  </tr>
  <tr>
    <td>Documento </td>
    <td>
      <input type="text" name="t_usr_nif" id="t_usr_nif">
    </td>
    <td>Celular</td>
    <td>
      <input type="text" name="t_usr_cel" id="t_usr_cel">
    </td>
  </tr>
  <tr>
    <td>Telefono</td>
    <td>
      <input type="text" name="t_usr_tel" id="t_usr_tel">
    </td>
    <td>Status</td>
    <td>
      <select name="cbo_usr_sts" id="cbo_usr_sts">
      <option value="1" selected="selected">Habilitado</option>
      <option value="0">Deshabilitado</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>F.Nacimiento</td>
    <td>
      <input type="text" class="tcal" name="t_usr_fna" id="t_usr_fna">
    </td>
    <td>F.Ingreso</td>
    <td>
      <input type="text" class="tcal" name="t_usr_fin" id="t_usr_fin">
    </td>
  </tr>
  <tr>
    <td>E-Mail</td>
    <td>
      <input type="text" name="t_usr_mai" id="t_usr_mai" />
    </td>
    <td>Multiples Tiendas:</td>
    <td>
      <select name="cbo_us_mul" id="cbo_us_mul">
      <option value="0">Deshabilitado</option>
      <option value="1">Habilitado</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>Local</td>
    <td>
      <select name="cbo_usr_cen" id="cbo_usr_cen">
      <?php 
	  include ('../Query_Min/getCentros.php');
	  ?>
      </select>
    </td>
    <td>Almacen</td>
    <td>
      <select name="cbo_usr_alm" id="cbo_usr_alm">
      <?php  echo $combo_alm;?>
      </select>
    </td>
  </tr>
  <tr>
    <td>Direccion</td>
    <td colspan="3">
      <input name="t_usr_dir" type="text" id="t_usr_dir" size="50">
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>
</div>

</div>


<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td valign="top" width="48%">
    <table width="100%" border="1" rules="all" class="tableIn tblSort" cellspacing="0" cellpadding="0">
    <caption>Lista General de Accesos</caption>
    <thead>
    <tr>
    <th>Descripci&oacute;n</th>
    <th>&nbsp;</th>
    </tr>
    </thead>
    <tbody>
    <?php 
	$sql = 'select ObjectID,ObjectTyp,ObjectUrl,ObjectDesc,ObjectGrp from tt_object order by ObjectDesc';
	$res = $mysqli->query($sql);
	if ($res){
	while($row = $res->fetch_assoc()){
	?>
    <tr title="<?php echo $row['ObjectID']; ?>">
    <td><?php echo $row['ObjectDesc']; ?></td>
    <td><input type="button" onclick="addAccUsr(this)" value=">>" /></td>
    </tr>
    <?php 
	}
	$res->close();
	$mysqli->check_result();
	}
	?>
    </tbody>
	</table>
</td>
<td width="4%">&nbsp;</td>
<td valign="top" id="d_obj_usr" width="48%">&nbsp;
<?php 

?>


</td>
</tr>
</table>    

 
<!-- tab "panes" -->




  <script type="text/javascript">
  
function updAutUsr(){
var usr = GEBI('t_usr_usr').value;
$('#d_obj_usr').loadx('Procesos/Opers/OperUsr.php',{ oper : 'getObjs' , user : usr});
}
  
  function addAccUsr(obj){
  	obj = $(obj).parent().parent();
	var cobj = obj.attr('title');
	var usr = GEBI('t_usr_usr').value;
	sendData('Procesos/Opers/OperUsr.php',
		{
		oper : 'aObj',
		obj : cobj,
		user : usr
		}
	);
  }
  
  function delAccUsr(obj){
	obj = $(obj).parent().parent();
	var cobj = obj.attr('title');
	var usr = GEBI('t_usr_usr').value;	
		sendData('Procesos/Opers/OperUsr.php',
		{
		oper : 'dObj',
		obj : cobj,
		user : usr
		}
	);
  }  
  function newUser(){
  GEBI('cbo_usr_cen').selectedIndex = 0;
  GEBI('cbo_usr_alm').selectedIndex = 0;
  GEBI('cbo_usr_sts').selectedIndex = 0;
  $('#t_usr_usr').enable();
  $('#d_dt_usr input[type=text]').val('').enable();
  }
function editUserSis(usr){
	var usr = $(usr).html();
	$("#d_dt_usr").css('opacity','0.5').disableInput(['input','selected','textarea']);
	sendData('Procesos/Opers/OperUsr.php',{ oper : 'view' , usuario : usr },
	function(){ 
	 $('#t_usr_usr').disable();
	GEBI('t_usr_usr').focus();self.location = '#d_dt_usr';
	$("#d_dt_usr").css('opacity','1.0').enableInput(['input','selected','textarea']);}
	);
	$('#d_obj_usr').loadx('Procesos/Opers/OperUsr.php',{ oper : 'getObjs' , user : usr});
}

function resetPWD(usr){
	if (confirm('Esta seguro de reestablecer la clave de acceso del usuario '+usr+'?')){	
		loadPopup({url : 'resetPwd.php', params : {  t_usuario : usr }});
	}
}

function saveUser(){
	if (!$("#tbl_dat_usr").checkInput()){
	return;
	}
	
	
	sendData('Procesos/Opers/OperUsr.php',{
		oper : 'upd',
		usr : GEBI('t_usr_usr').value,
		nom : GEBI('t_usr_nom').value,
		ape : GEBI('t_usr_ape').value,
		dir : GEBI('t_usr_dir').value,
		nif : GEBI('t_usr_nif').value,
		mai : GEBI('t_usr_mai').value,
		cel : GEBI('t_usr_cel').value,
		tel : GEBI('t_usr_tel').value,
		mai : GEBI('t_usr_mai').value,
		cen : GEBI('cbo_usr_cen').value,
		alm : GEBI('cbo_usr_alm').value,
		sts : GEBI('cbo_usr_sts').value,
		fna : GEBI('t_usr_fna').value,
		fin : GEBI('t_usr_fin').value,
		mul : GEBI('cbo_us_mul').value
	});
}

function updListaUsr(){
	$('#d_tbl_usr').loadx('Procesos/Opers/OperUsr.php',{oper : 'list'});
}
</script>
</p>
